package com.spring.springboot.controller;

import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.springboot.entity.UserEntity;
import com.spring.springboot.jpa.UserJPA;


/**
*@author LSY
*Time: 2018年12月7日下午4:37:01
*
*
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserJPA userJpa;
	
	@RequestMapping(value = "all", method = RequestMethod.GET)
	@ResponseBody
	public List<UserEntity> getAll(){
		return userJpa.findAll();
	}
	
	@RequestMapping(value = "/login")
    public String login(UserEntity user, HttpServletRequest request)
    {
        //登录成功
        boolean flag = true;
        String result = "登录成功";
        
        UserEntity userEntity = userJpa.getOne(user.getUserName());
        //用户不存在
        if(userEntity == null){
            flag = false;
            result = "用户不存在，登录失败";}
        //密码错误
        else if(!userEntity.getName().equals(user.getName())){
            flag = false;
            result = "用户密码不相符，登录失败";
        }
        //登录成功
        if(flag){
            //将用户写入session
            request.getSession().setAttribute("user",userEntity);
        }
        return result;
    }
}
